<?php
/**
 * Created by PhpStorm.
 * User: 吴登科
 * Date: 2018/10/24
 * Time: 20:01
 * 店铺公告通知消息管理
 */

namespace Admin\Controller;

class NoticeController extends PublicController
{
    function _initialize()
    {
        parent::_initialize();
    }
    /**
     * 列表
     */
    public function index()
    {
        $wadmin = session("wadmin");//店铺后台管理员信息
//       print_r($wadmin);
        //判断是否为超级管理员操作
        if ($wadmin['store_id'] == 0) {
            $where = '';
        } else {
            $where['n.store_id'] = $wadmin['store_id'];
        }

        $count = M('notice')->alias("n")->where($where)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('theme', "<ul class='pagination no-margin pull-right'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");
        $show = $Page->show(); // 分页显示输出

        $result = M('notice')->alias("n")
            ->field('n.*,s.store_name')
            ->join("meal_store as s on n.store_id=s.store_id", 'left')
            ->where($where)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order("n.id desc")
            ->select();

        foreach ($result as $key => $value) {
            if (!$value['modify_time']) {
                $result[$key]['modify_time'] = '暂无修改';
            }
        }

//        var_dump($result);die;

        $this->assign("page", $show); // 赋值分页输出
        $this->assign("result", $result);
        $this->display();
    }

    /**
     * 添加编辑页面
     */
    public function add_edit()
    {
        $wadmin = session("wadmin");
        if ($wadmin['store_id'] == 0) {
            $where = '';
        } else {
            $where['store_id'] = $wadmin['store_id']; // 店铺ID
        }

        $id = I('get.id');
        $result = M('notice')->where("id='$id'")->find();
        $store = M('store')->select();

        $this->assign('result', $result);
        $this->assign('storeId', $wadmin['store_id']);
        $this->assign('store', $store);
        $this->display();
    }

    /**
     * 添加或编辑操作
     */
    public function save()
    {
        $wadmin = session("wadmin");

        $id = I('request.id');
        $data['content'] = I('post.content');
        $data['is_show'] = I('post.is_show');

        if ($wadmin['store_id'] == 0) {
            $data['store_id'] = I('post.store_id');
        } else {
            $data['store_id'] = $wadmin['store_id'];
        }

        if ($id) {
            //修改
            $result = M('notice')->where("id='$id'")->save($data);
        } else {
            // 新增
            $data['create_time'] = time();
            $data['create'] = $wadmin['user_name'];
            $result = M('notice')->add($data);
        }
        if ($result) {
            $this->success("操作成功", U('Admin/Notice/index'));
        } else {
            $this->error("操作失败", U('Admin/Notice/index'));
        }
    }

    /**
     * 删除
     */
    public function del()
    {
        $id = I('get.id');
        //查找该菜品的分类
        if ($id) {
            $result = M('notice')->delete($id);
            if ($result) {
                $this->success("删除成功", U('Admin/Notice/index'));
            } else {
                $this->error("删除失败", U('Admin/Notice/index'));
            }
        }
    }
}